package myservice.android.utilities;

import java.util.ArrayList;

import myservice.android.Global;
import myservice.android.R;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class RecordAdapter extends ArrayAdapter<Record> {
	
		private Context context;
        private ArrayList<Record> items;
        private int type; // 0=interested ones, 1=calls with person name, 2=publications, 3=territory elements

        public RecordAdapter(Context context, int textViewResourceId, ArrayList<Record> items, int type) {
                super(context, textViewResourceId, items);
                this.items = items;
                this.context=context;
                this.type=type;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
                View v = convertView;
                if (v == null) {
                    LayoutInflater vi = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                    v = vi.inflate(R.layout.list_row, null);
                }                
                
                Record o = items.get(position);
                if (o != null) {
                        	TextView firstLine = (TextView) v.findViewById(R.id.firstLine);
                        	TextView secondLine = (TextView) v.findViewById(R.id.secondLine);
                        	ImageView icon = (ImageView) v.findViewById(R.id.icon);
                        	ImageView completedIcon = (ImageView) v.findViewById(R.id.completedIcon);
                        	
                        	if (o.completed==1)
                        		completedIcon.setVisibility(View.VISIBLE);
                        	else
                        		completedIcon.setVisibility(View.GONE);
                        
                        	if (type!=2)
                        		firstLine.setText(o.firstLine);
                        	else {
                        		firstLine.setVisibility(View.GONE);
                        	}
                        	
                        	secondLine.setText(o.secondLine);
                        	
                        	if (type==0) { //interested ones
                        		if (o.status==0)
                        			icon.setImageDrawable(Global.res.getDrawable(R.drawable.person_inactive));
                        		else if (o.status==1)
                        			icon.setImageDrawable(Global.res.getDrawable(R.drawable.person_interested));
                        		else if (o.status==2)
                        			icon.setImageDrawable(Global.res.getDrawable(R.drawable.person_magreader));
                        		else if (o.status==3)
                        			icon.setImageDrawable(Global.res.getDrawable(R.drawable.person_study));
                        	}
                        	else if (type==1) { //calls
                        		if (o.status==0)
                        			icon.setImageDrawable(Global.res.getDrawable(R.drawable.call_icon));
                        		else if (o.status==1)
                        			icon.setImageDrawable(Global.res.getDrawable(R.drawable.bible_study));
                        	}
                        	else if (type==2) { //publications
                        		if (o.status==0)
                        			icon.setImageDrawable(Global.res.getDrawable(R.drawable.magazine));
                        		else if (o.status==1)
                        			icon.setImageDrawable(Global.res.getDrawable(R.drawable.book));
                        		else if (o.status==2)
                        			icon.setImageDrawable(Global.res.getDrawable(R.drawable.tract));
                        		else if (o.status==3)
                        			icon.setImageDrawable(Global.res.getDrawable(R.drawable.video));
                        		else if (o.status==4)
                        			icon.setImageDrawable(Global.res.getDrawable(R.drawable.brochure));                        		
                        	}     
                        	else if (type==3) { //territory elements                        		
                        		if (o.status==0)
                        			icon.setImageDrawable(Global.res.getDrawable(R.drawable.territory));
                        		else if (o.status==1)
                        			icon.setImageDrawable(Global.res.getDrawable(R.drawable.street));
                        		else if (o.status==2)
                        			icon.setImageDrawable(Global.res.getDrawable(R.drawable.house));
                        		else if (o.status==3)
                        			icon.setImageDrawable(Global.res.getDrawable(R.drawable.apartment_house));
                        		else if (o.status==4)
                        			icon.setImageDrawable(Global.res.getDrawable(R.drawable.staircase));
                        		else if (o.status==5)
                        			icon.setImageDrawable(Global.res.getDrawable(R.drawable.apartment));     
                        		else if (o.status==6)
                        			icon.setImageDrawable(Global.res.getDrawable(R.drawable.apartment_visited));
                        		else if (o.status==7)
                        			icon.setImageDrawable(Global.res.getDrawable(R.drawable.house_visited));                         		
                        		
                        	}                            	
                }
                return v;
        }
        
}
